/**
 * 1.新增js文件
 * 2.重命名js文件，比如重命名为func.js
 * 3.初始化func.js脚本，初始化为以下格式
 */
(function () {
  'use strict';
var headUrl = "http://shineholder.wicp.net";
var tableId = "BD_MATERIAL";
var moduleName = "物料管理";
let Fid = 0;
  // 定义你的函数
  function sycns(ctx,loginDatas) {
	
	
	var data =
					//:\"FPayCurrencyId\",\"Message\":\"字段“结算币别”是必填项\",\"DIndex\":0},{\"FieldName\":\"FUseOrgId\",\"Message\":\"字段“使用组织”是必填项\",\"DIndex\":0},{\"FieldName\":\"FCreateOrgId\",\"Message\":\"字段“创建组织”是必填项\
					{
						"NeedUpDateFields": [],
						"NeedReturnFields": [],
						"IsDeleteEntry": "true",
						"SubSystemId": "",
						"IsVerifyBaseDataField": "false",
						"IsEntryBatchFill": "true",
						"ValidateFlag": "true",
						"NumberSearch": "true",
						"IsAutoAdjustField": "false",
						"InterationFlags": "",
						"IgnoreInterationFlag": "",
						"Model": {
							"FCUSTID": 0,
							"FCreateOrgId": {
								"FNumber": `${ctx.ctx.record.创建组织的id}`
							},
							"FUseOrgId": {
								"FNumber": `${ctx.ctx.record.创建组织的id}`
							},
							"FName": `${ctx.ctx.record.客户名称}`,
							"FCOUNTRY": {
								"FNumber": "Chengdu"
							},
							"FINVOICETITLE": `${ctx.ctx.record.客户名称}`,
							"FIsGroup": false,
							"FIsDefPayer": false,
							"FCustTypeId": {
								"FNumber": `${ctx.ctx.record.客户类别的编码}`
							},
							"FGroup": {
								"FNumber": `${ctx.ctx.record.客户分组编码}`
							}
						}
					}
	
	
	
				//请求k3的保存接口
				// console.log(JSON.stringify(data))
				//  这是一个request请求
				const loginResp = informat.http.request({
					method: "POST",
					url: headUrl +
						"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
					timeout: 30000,
					header: {
						"Content-Type": "application/json",
					},
					body: loginDatas
				});
				//JSON.parse() 方法是数据转换为 JS 对象
				console.log("loginResp")
				const loginData = JSON.parse(loginResp.content);
				const contentResp = informat.http.request({
					method: "POST",
					url: headUrl +
						"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc",
					timeout: 30000,
					header: {
						"Content-Type": "application/json",
						"kdservice-sessionid": loginData.KDSVCSessionId
					},
					//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
					body: JSON.stringify({
						"formid": tableId,
						data
					})
				});
				console.log("contentResp---->", contentResp)
				const contentData = JSON.parse(contentResp.content);
				//获取到保存的Id
				var Id = contentData.Result.Id;
				Fid = Id
				console.log("Fid----》", Id)
				//
				var requestDate = {
					"CreateOrgId": 0,
					"Numbers": [],
					"Ids": `${Id}`,
					"SelectedPostId": 0,
					"NetworkCtrl": "",
					"IgnoreInterationFlag": ""
				}
				//console.log(requestDate)
				//请求k3的提交接口
				const rps = informat.http.request({
					method: "POST",
					url: headUrl +
						"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc",
					timeout: 30000,
					header: {
						"Content-Type": "application/json",
						"kdservice-sessionid": loginData.KDSVCSessionId
					},
					//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
					body: JSON.stringify({
						"formid": tableId,
						"data": requestDate
					})
				});
				console.log('ctx---->', Object.keys(ctx.ctx));
	
				var DataId = ctx.ctx.id
				console.log(DataId)
				var table = informat.table.getTable(moduleName);
				console.log("table->", table)
				var fieldList = informat.table.getFieldList(table.id);
				console.log(Fid)
				var bean = {
					'k3id': Fid
				}
				// console.log(fieldList)
				bean = informat.table.convert(fieldList, bean)
				informat.table.update(table.id, DataId, bean);
				//   console.log(resopen)
				//console.log("contentData====>",contentData.Result.Id)
	
				// console.log("errors====>",contentData.Result.ResponseStatus.Errors)
				/**      if (resp.code !== 200) {
	                    throw new Error('同步失败');
	                }
	*/
        	};
		
  function update(ctx,base_service,sql_utils,loginDatas){
      console.log("调用修改方法了")
	   // console.log(sql_utils.convertId2Alias(ctx.tableFieldList, ctx.updateData))
	   var insertData = sql_utils.convertId2Alias(ctx.tableFieldList, ctx.newRecord)
	   var k3id = insertData.k3id
	   var data =
	   	//:\"FPayCurrencyId\",\"Message\":\"字段“结算币别”是必填项\",\"DIndex\":0},{\"FieldName\":\"FUseOrgId\",\"Message\":\"字段“使用组织”是必填项\",\"DIndex\":0},{\"FieldName\":\"FCreateOrgId\",\"Message\":\"字段“创建组织”是必填项\
	   	{
	   		"NeedUpDateFields": [],
	   		"NeedReturnFields": [],
	   		"IsDeleteEntry": "true",
	   		"SubSystemId": "",
	   		"IsVerifyBaseDataField": "false",
	   		"IsEntryBatchFill": "true",
	   		"ValidateFlag": "true",
	   		"NumberSearch": "true",
	   		"IsAutoAdjustField": "false",
	   		"InterationFlags": "",
	   		"IgnoreInterationFlag": "",
	   		"Model": {
	   			"FSupplierId": k3id,
	   			"FGroup": {
	   				"FNumber": `${insertData.FGroup}`
	   			},
	   			"FCreateOrgId": {
	   				"FNumber": `${insertData.FCreateOrgId}`
	   			},
	   			"FUseOrgId": {
	   				"FNumber": `${insertData.FUseOrgId}`
	   			},
	   			"FName": insertData.FName,
	   			"FCustTypeId": {
	   				"FNumber": insertData.FCustTypeId
	   			},
	   		}
	   	}
	   
	   
	   var fh = {
	   	"CreateOrgId": 0,
	   	"Numbers": [],
	   	"Ids": `${k3id}`,
	   	"InterationFlags": "",
	   	"IgnoreInterationFlag": "",
	   	"NetworkCtrl": "",
	   	"IsVerifyProcInst": ""
	   }
	   const loginResp = informat.http.request({
	   	method: "POST",
	   	url: headUrl +
	   		"/k3cloud/Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
	   	timeout: 30000,
	   	header: {
	   		"Content-Type": "application/json",
	   	},
	   	body: loginDatas
	   });
	   //JSON.parse() 方法是数据转换为 JS 对象
	   console.log("loginResp")
	   const loginData = JSON.parse(loginResp.content);
	   
	   //反审核
	   const contentResp = informat.http.request({
	   	method: "POST",
	   	url: headUrl +
	   		"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc",
	   	timeout: 30000,
	   	header: {
	   		"Content-Type": "application/json",
	   		"kdservice-sessionid": loginData.KDSVCSessionId
	   	},
	   	//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
	   	body: JSON.stringify({
	   		"formid": tableId,
	   		"data": fh
	   	})
	   });
	    console.log("contentResp---->",contentResp)
	   
	   //k3 接口修改
	   
	   
	   const updateDate = informat.http.request({
	   	method: "POST",
	   	url: headUrl +
	   		"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc",
	   	timeout: 30000,
	   	header: {
	   		"Content-Type": "application/json",
	   		"kdservice-sessionid": loginData.KDSVCSessionId
	   	},
	   	//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
	   	body: JSON.stringify({
	   		"formid": tableId,
	   		"data": data
	   	})
	   });
	   
	   console.log('updateDate----->', updateDate);
	   
	   var requestDate = {
	   	"CreateOrgId": 0,
	   	"Numbers": "",
	   	"Ids": `${k3id}`,
	   	"SelectedPostId": 0,
	   	"NetworkCtrl": "",
	   	"IgnoreInterationFlag": ""
	   }
       console.log(insertData)
	   //k3 的提交
	   //请求k3的提交接口
	   const rps = informat.http.request({
	   	method: "POST",
	   	url: headUrl +
	   		"/K3Cloud/Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc",
	   	timeout: 30000,
	   	header: {
	   		"Content-Type": "application/json",
	   		"kdservice-sessionid": loginData.KDSVCSessionId
	   	},
	   	//JSON.stringify() 方法是 JS 值转换为 JSON 字符串的   
	   	body: JSON.stringify({
	   		"formid": tableId,
	   		"data": requestDate
	   	})
	   });
	   console.log(rps)
  }

  return {
    update: update,
        sycns:sycns
  };
})();

